﻿///////////////////////////////////////////////////////////////////////////////
//
// This file is part of the BassDrop project
//   https://code.google.com/p/bass-drop/
//
//
// Creator:	Gergely Krajcsovszki (krajcsovszkig@hotmail.com)
// Date:	2013-08-05 12:30
//
//
// Copyright © 2013-2014. Gergely Krajcsovszki
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
///////////////////////////////////////////////////////////////////////////////
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BassDrop.Core.Common.Options;
using BassDrop.Core.Common.Services;

namespace BassDrop.Core.Common.Plugins
{
	/// <summary>
	/// Abstract base class for plugin option lists. Contains the set of OptionsItems.
	/// </summary>
	public abstract class PluginOptionsSetBase : OptionsSetBase
	{
		/// <summary>
		/// The plugin whose options these are
		/// </summary>
		public BassDropPluginBase Plugin
		{
			get;
			private set;
		}

		/// <summary>
		/// The plugin instance whose options these are or null if they're for the plugin
		/// </summary>
		public PluginObjectBase Instance
		{
			get;
			private set;
		}

		/// <summary>
		/// The name of this options set
		/// </summary>
		public override string Name
		{
			get
			{
				if ( Instance != null )
				{
					return Instance.InstanceName;
				}
				else
				{
					return Plugin.Name;
				}
			}
		}

		/// <summary>
		/// Constructs a new options set
		/// </summary>
		/// <param name="plugin">the plugin whose options these are</param>
		/// <param name="instance">the plugin instance whose options these are or null if they're for the plugin</param>
		protected PluginOptionsSetBase( BassDropPluginBase plugin, PluginObjectBase instance )
		{
			Plugin = plugin;
			Instance = instance;
		}

		/// <summary>
		/// Loads the options
		/// </summary>
		/// <param name="settings">the seettings object to load from</param>
		public override async Task Load( ISettings settings )
		{
			if ( settings != null )
			{
				await SetOptions( settings.PluginSettings( Plugin.Name, (Instance != null ? Instance.InstanceName : null) ) );
			}
		}

		/// <summary>
		/// Saves the options
		/// </summary>
		/// <param name="settings">the seettings object to save to</param>
		public override void Save( ISettings settings )
		{
			IDictionary<string, string> options = GetOptions();

			foreach ( var option in options.Keys )
			{
				settings.SetPluginSettingAt( option, options[option], Plugin.Name, (Instance != null ? Instance.InstanceName : null) );
			}

			settings.Save();
		}

		/// <summary>
		/// Returns the options of this item
		/// </summary>
		/// <returns></returns>
		protected abstract IDictionary<string, string> GetOptions();

		/// <summary>
		/// Sets the options of this item
		/// </summary>
		/// <param name="dictionary"></param>
		protected abstract Task SetOptions( IDictionary<string, string> dictionary );
	}
}
